<template>
  <div class="md-goods-detail">
    <div class="md-goods-info-item">
      <div class="item-left">
        <md-swipergd height="350" v-if="productInfo.picture" :swiperData="productInfo.picture" />
      </div>
      <div class="item-right">
        <p class="item-title">{{ productInfo.title ? productInfo.title: '--' }}</p>
        <p class="item-description" v-if="productInfo.description">{{productInfo?.description}}</p>
        <div class="total-box">
          <p class="total-num">price: {{ productInfo.seliingPrice ? `$${parseFloat(productInfo.seliingPrice || 0).toFixed(2)}`: '--' }}</p>
          <p class="total-num">stocks: {{ productInfo.quantity ? productInfo.quantity : '--' }}</p>
        </div>
        <div class="item-btns">
          <div class="item-countdown" @click="clickBuy">Product Purchase Address</div>
          <div class="free-btn" @click.stop="handleApply">Free Trial</div>
        </div>
      </div>
    </div>
    <div class="md-goods-description">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="experience illustrate" name="first">
          <div v-html="productInfo.illustrate" class="product-desc"></div>
          <div class="title">product information</div>
          <div v-html="productInfo.content" class="product-desc"></div>
        </el-tab-pane>
        <!-- <el-tab-pane label="product information" name="second">
          <div v-html="productInfo.content" class="product-desc"></div>
        </el-tab-pane> -->
        <el-tab-pane label="experience product" name="third">
          <div>
            <md-goods-list :loading="infoLoading" :goodsInfo="productDataList" />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <md-apply-product-dialog :flag="flag" :dialogInfo="dialogInfo" @close="flag = false"></md-apply-product-dialog>
  </div>
</template>
<script>
export default {
  name: 'MDGoodsDetailComponent',
  layout: 'dynamic',
  // async asyncData (ctx) {
  //   // console.log(ctx.params.productId) // ctx.params.xxx即可获得nuxt-link里传递的参数
  //   console.log(this)
  // },
  data () {
    return {
      activeName: 'first',
      productId: null,
      productInfo: { // 产品info
        title: '',
        picture: []
      },
      infoLoading: false,
      productDataList: [], // 体验列表
      flag: false,
      dialogInfo: {
        userId: '',
        productId: '',
        status: '2' // 待审核
      }
    }
  },
  async created () {
    this.productId = this.$route.query.productId
    this.getProductDetail(this.productId)
  },
  methods: {
    async getProductDetail (productId) {
      const res = await this.$axios.get(`web/product/${productId}`)
      if (res.code == 200) {
        console.log(res.data)
        this.productInfo = res.data || {}
        this.productInfo.picture = JSON.parse(res.data.picture)
      }
    },
    async handleClick () {
      if (this.activeName === 'third') {
        const options = {
          productId: this.$route.query.productId, // 产品类型（1新品 2往期 3体验）
          pageIndex: 1,
          pageSize: 100000
        }
        this.infoLoading = true
        const res = await this.$axios.post('web/product/experienceList', options)
        this.infoLoading = false
        if (res.code == 200) {
          this.productDataList = res.data
        }
      }
    },
    clickBuy () {
      window.open(this.productInfo.purchaseLink)
    },
    async handleApply (item) {
      const user = this.$local.getItem('user')
      if (user) { // 已经登录
        this.dialogInfo.userId = user.userId
        this.dialogInfo.productId = this.$route.query.productId
        const res = await this.$axios.get(`web/userExperiencer/check/apply/${user.userId}`)
        if (res.code == 200) {
          if (res.data) { // 已经申请过，提示申请理由
            // this.$message.warning('you applied!')
            this.flag = true
          } else { // 没申请跳转到 申请页
            let u = `/experiencer/apply?path=${this.$route.path}`
            if (this.$getParamsStr(this.$route.query)) {
              u += `&${this.$getParamsStr(this.$route.query)}`
            }
            this.$router.push(u)
          }
        }
      } else {
        this.$router.push({
          path: '/login',
          query: {
            turnBackPath: location.href
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.md-goods-detail {
  width: 100%;
  background-color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 28px;
  .md-goods-info-item {
    width: 1200px;
    box-sizing: border-box;
    margin-bottom: 15px;
    padding-bottom: 20px;
    background-color: #fff;
    border-bottom: 1px solid #efefef;
    display: flex;
    .item-left {
      width: 50%;
      height: 350px;
      margin-right: 30px;
    }
    .item-right {
      width: calc(50% - 50px);
      height: 350px;
      color: #414141;
      position: relative;
      .item-title {
        @include block-ell(2);
        font-size: 28px;
      }
      .item-description {
        color: #666;
        font-size: 16px;
        line-height: 1.5;
        margin-top: 10px;
      }
      .total-box {
        display: flex;
        align-items: center;
        margin-top: 40px;
        // justify-content: center;
        .total-num {
          height: 30px;
          line-height: 30px;
          text-align: center;
          border-radius: 2px;
          font-size: 20px;
          color: #414141;
          &:first-child {
            margin-right: 50px;
          }
        }
      }
      .item-btns {
        width: 100%;
        display: flex;
        align-items: center;
        position: absolute;
        bottom: 0px;
        left: 0px;
        z-index: 10;
      }

      .item-countdown {
        width: 200px;
        border: 1px solid $md-primary-color;
        height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        align-self: center;
        border-radius: 4px;
        cursor: pointer;
        color: $md-primary-color;
        &:hover {
          background: $md-primary-color;
          color: #fff;
        }
      }
      .free-btn {
        width: 150px;
        border: 1px solid $md-primary-color;
        height: 40px;
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        align-self: center;
        border-radius: 4px;
        background: $md-primary-color;
        cursor: pointer;
        color: #fff;
        margin-left: 30px;
      }
    }
  }
  .md-goods-description {
    width: 1200px;
    box-sizing: border-box;

    .title {
      font-size: 14px;
      font-weight: 500;
      color: #6ac08f;
      margin-top: 15px;
      margin-bottom: 15px;
    }
  }
  .el-tab-pane {
    &:nth-child(2) {
      padding-top: 20px;
      padding-left: 10px;
    }
    ::v-deep .product-desc {
      p {
        font-size: 16px !important;
        color: #444 !important;
        line-height: 1.5 !important;
        margin-bottom: 10px;
        strong {
          margin: 10px 0;
        }
      }
      img {
        width: 100%;
        object-fit: contain;
        // margin-bottom: 10px;
      }
      ul {
        margin-top: 10px;
        li {
          font-size: 14px !important;
          color: #444 !important;
          line-height: 1.5 !important;
          margin-bottom: 10px;
          span {
            font-size: 16px !important;
            color: #444 !important;
            line-height: 1.5 !important;
            margin-bottom: 10px;
          }
        }
      }
    }
  }
}
</style>
